<?php
class Warehouse_Model_Products
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Warehouse_Model_DbTable_Products();
	}
	
	public function getAdapter(){
		return $this->_table->getAdapter();
	}
	
	/**
	*Create A Record  
	*@param rowSet $row Department's data 
	*@return boolean
	*/
	public  function add($row){	 
 		return $this->_table->insert($row); 
	}
	
	/**
	*Update One Row  
	*@param rowSet $row Department's data 
	*@param int $ud_id 
	*@return boolean
	*/
	public  function update($row,$id){
        $row['wh_last_update']=date('Y-m-d H:i:s');
		$where = $this->_table->getAdapter()->quoteInto('wp_id = ?', $id);
		return $this->_table->update($row, $where);	
	}
	
	/**
	 * 根据字段值更新
	 */
	public  function stocktakeUpdate($row,$field,$value){
		$where = $this->_table->getAdapter()->quoteInto("{$field}= ?", $value);
		return $this->_table->update($row, $where);	
	}
		
	/**
	*No Delete One Row
	*@param int $ud_id 
	*@return boolean
	*/
	 public function delete($id){	
		$where = $this->_table->getAdapter()->quoteInto('wp_id = ?', $id); 
		return $this->_table->delete($where);
	}
	
	public function getByWpid($wp_id){	
		$select = $this->_table->select()->where('wp_id = ?', $wp_id);	 
		return $this->_table->fetchRow($select)->toArray();		
	}

    public function getByProductId($ProductId, $warehouseId = 1)
    {
        $select = $this->_table->select()->where('product_id = ?', $ProductId)
                                         ->where('warehouse_id = ?', $warehouseId)
                                         ->order(array('wp_id desc'));
        return $this->_table->fetchRow($select);
    }
	
    public function getByAllProductId($ProductId,$warehouseId=1){
   		$select = $this->_table->select()->where('product_id = ?', $ProductId)
                                         ->where('warehouse_id = ?', $warehouseId);                                   
   		return $this->_table->getAdapter()->fetchAll($select);
   	}
   	
   	public function getByInProductIds($ProductIds,$warehouseId=1){
   		$select = $this->_table->select()->where('product_id in (?)', $ProductIds)
   		->where('warehouse_id = ?', $warehouseId);
   		return $this->_table->getAdapter()->fetchAll($select);
   	}
	
	public function getJoinByCond($cond,$type,$order='wp_id',$pageSize=0,$page=1){
		
//		if(isset($cond['pp_name']) && $cond['pp_name']){
//			$select->where('pp_name = ?',$cond['pp_name']);
//		}
		$select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,"*");
        $select->join('warehouse_shelf', 'warehouse_products.ws_code = warehouse_shelf.ws_code', '*');
        $select->join('product', 'warehouse_products.product_id = product.product_id', '*'); 
        $select->where("1 =?",1);
		if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($order);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();

		    return $this->_table->getAdapter()->fetchAll($sql);
        }
        
	}
	
    public function getByJoinWSCondition($condition=array(),$type='*',$orderby=array('ws.wp_id'),$pageSize=0,$page=0){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table." as wp",$type);
		$select->joinLeft('warehouse_shelf as ws', 'ws.ws_code = wp.ws_code', 'ws.wa_code');
        $select->where("1 =?",1);
		if(isset($condition["wa_code"]) && $condition["wa_code"]!=''){
            $select->where(" ws.wa_code = ?",$condition["wa_code"]);
        }
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
		
	}
	
	public function getByJoinInWsCodeCon($condition=array(),$type='*',$orderby=array('ws.wp_id'),$pageSize=0,$page=0){
		$select = $this->_table->getAdapter()->select();
		$table = $this->_table->info('name');
		$select->from($table." as wp",$type);
		$select->joinLeft('warehouse_shelf as ws', 'ws.ws_code = wp.ws_code', 'ws.ws_id');
		$select->where("1 =?",1);
		if(isset($condition["ws_code"]) && $condition["ws_code"]!=''){
			$select->where(" ws.ws_code in(?)",$condition["ws_code"]);
		}
		if('count(*)'==$type){
			return $this->_table->getAdapter()->fetchOne($select);
		}else{
			$select->order($orderby);
			if($pageSize>0 and $page>0)
			{
				$start =($page-1)*$pageSize;
				$select->limit($pageSize,$start);
			}
			$sql=$select->__toString();
			return $this->_table->getAdapter()->fetchAll($sql);
		}
	
	}
	
	
    public function getByCond($condition=array(),$type='*',$orderby=array('ws_code'),$pageSize=0,$page=1)
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
        if (isset($condition["warehouse_id"]) && $condition["warehouse_id"] != '') {
            $select->where(" warehouse_id = ?", $condition["warehouse_id"]);
        }
        if (isset($condition["product_id"]) && $condition["product_id"] != '') {
            $select->where(" product_id = ?", $condition["product_id"]);
        }
        if (isset($condition["customer_id"]) && $condition["customer_id"] != '') {
            $select->where(" customer_id = ?", $condition["customer_id"]);
        }
        if (isset($condition["ws_code"]) && $condition["ws_code"] != '') {
            $select->where(" ws_code = ?", $condition["ws_code"]);
        }
        if (isset($condition["wp_status"]) && $condition['wp_status'] != "") {
            $select->where("wp_status=?", $condition['wp_status']);
        }
        if ('count(*)' == $type) {
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
	}
	
	public function getByCondition($condition=array()){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,'*');
        $select->where("1 =?", 1);
        if(!isset($condition["warehouse_id"])){
            $condition["warehouse_id"]=1;
        }
        if (isset($condition["warehouse_id"]) && $condition["warehouse_id"] != '') {
            $select->where(" warehouse_id = ?", $condition["warehouse_id"]);
        }
        if (isset($condition["product_id"]) && $condition['product_id'] != "") {
            $select->where("product_id=?", $condition['product_id']);
        }
        if (isset($condition["ws_code"]) && $condition['ws_code'] != "") {
            $select->where("ws_code=?", $condition['ws_code']);
        }
        if (isset($condition["wp_status"]) && $condition['wp_status'] != "") {
            $select->where("wp_status=?", $condition['wp_status']);
        }
        if (isset($condition["customer_id"]) && $condition['customer_id'] != "") {
            $select->where("customer_id=?", $condition['customer_id']);
        }
		return $this->_table->getAdapter()->fetchRow($select);
	}
	
	public function getByConditionAll($condition=array()){
		$select = $this->_table->getAdapter()->select();
		$table = $this->_table->info('name');
		$select->from($table,'*');
		$select->where("1 =?", 1);
		if(!isset($condition["warehouse_id"])){
			$condition["warehouse_id"]=1;
		}
		if (isset($condition["warehouse_id"]) && $condition["warehouse_id"] != '') {
			$select->where(" warehouse_id = ?", $condition["warehouse_id"]);
		}
		if (isset($condition["product_id"]) && $condition['product_id'] != "") {
			$select->where("product_id=?", $condition['product_id']);
		}
		if (isset($condition["ws_code"]) && $condition['ws_code'] != "") {
			$select->where("ws_code=?", $condition['ws_code']);
		}
		if (isset($condition["wp_status"]) && $condition['wp_status'] != "") {
			$select->where("wp_status=?", $condition['wp_status']);
		}
		
		if (isset($condition["wp_quantity"]) && $condition['wp_quantity'] != "") {
			$select->where("wp_quantity=?", $condition['wp_quantity']);
		}
		if (isset($condition["wp_quantity_hold"]) && $condition['wp_quantity_hold'] != "") {
			$select->where("wp_quantity_hold=?", $condition['wp_quantity_hold']);
		}
		
		if (isset($condition["customer_id"]) && $condition['customer_id'] != "") {
			$select->where("customer_id=?", $condition['customer_id']);
		
		}
		return $this->_table->getAdapter()->fetchAll($select);
	}
	
	public function lockForUpdate($condition=array()){
        if(!isset($condition["warehouse_id"])){
           $condition["warehouse_id"]=1;
        }
		$sql = "select * from warehouse_products";
		$where=" where 1=1";
		if(isset($condition["product_id"]) && $condition['product_id'] != ""){
			$where.=" and product_id=".$condition['product_id'];
		}
		if(isset($condition["ws_code"]) && $condition['ws_code'] != ""){
			$where.=" and ws_code='".$condition['ws_code']."' ";
		}
        if(isset($condition["warehouse_id"]) && $condition['warehouse_id'] != ""){
        			$where.=" and warehouse_id='".$condition['warehouse_id']."' ";
        }
		$sql.=$where." for update";
		return $this->_table->getAdapter()->fetchRow($sql);
	}
	
	/**
	 * 出货更新冻结库存量
	 * 
	 */
	public  function updateQuantityHold($row,$awhere){
		//$strWhere=implode(' AND ',$awhere);
		
		$where = $this->_table->getAdapter()->quoteInto('1= ?', 1);
		foreach ($awhere as $key=>$val){
			$where .=$this->_table->getAdapter()->quoteInto(" AND {$key} = ?", $val);
		}
		return $this->_table->update($row, $where);
	}
	
	public function getByfield($field,$value,$type=2){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);	
		if($type == 1){
			return $this->_table->fetchRow($where);
		}else {
			return $this->_table->fetchAll($where);
		}
		
	}
	
	
	/**
	 * 针对库存盘点读取数据
	 * $cond查询条件， 索引为数据表的字段
	 * @return 二维数组
	 */
	public function getForStocktake($cond,$type,$pageSize=0,$page=0,$orderby=array()){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');	// $table : warehouse_products
        if($type=='*'){
        	$select->from($table,'*');
        	$select->join('product', $table.'.product_id = product.product_id', array('product.product_sku','product.customer_id'));
        	$select->join('warehouse_shelf', $table.'.ws_code = warehouse_shelf.ws_code', array('warehouse_shelf.wa_code'));
        }else {
        	$select->from($table,'count(distinct warehouse_products.ws_code) as total');
        	$select->join('product', $table.'.product_id = product.product_id','');
        	$select->join('warehouse_shelf', $table.'.ws_code = warehouse_shelf.ws_code', '');
        }
        $select->where("1 =?",1);
        $select->where( $table.".wp_quantity_hold = ?",0);	//hold 数量为 0 才能盘点
        $select->where( $table.".wp_status != ?",2);		
        
		if(isset($cond["warehouse_id"]) && $cond["warehouse_id"]!=''){
            $select->where( $table.".warehouse_id = ?",$cond["warehouse_id"]);
        }
        if(isset($cond["ws_code"]) && $cond["ws_code"]!=''){
            $select->where( $table.".ws_code like ?",$cond["ws_code"].'%');
        }
        if(isset($cond["wp_status"]) && $cond["wp_status"]!=''){
            $select->where( $table.".wp_status = ?",$cond["wp_status"]);
        }
		if(isset($cond['product_sku']) && $cond['product_sku']!=''){
            $select->where( "product.product_sku like ?",$cond['product_sku'].'%');
        }
        if(isset($cond["customer_id"]) && $cond["customer_id"]!=''){
        	$select->where("product.customer_id = ?",$cond["customer_id"]);
        }
        if(isset($cond['wa_code']) && $cond['wa_code']!=''){
        	$select->where("warehouse_shelf.wa_code = ?",$cond['wa_code']);
        }                
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0){
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
//			print_r($sql);exit();
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
		
	}
	
	
}